package com.xiaoge.system.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoge.handler.BusinessException;
import com.xiaoge.response.Result;
import com.xiaoge.response.ResultCode;
import com.xiaoge.system.entity.TbUser;
import com.xiaoge.system.service.TbUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author czq
 * @since 2020-10-22
 */
@RestController
@RequestMapping("/user")
@Api(value = "系统用户模块",tags = "系统用户接口")   //用于生成接口文档
public class TbUserController{

    @Autowired
    private TbUserService tbUserService;

    // 分页查询用户列表
    @GetMapping("/findUserList")
    @ApiOperation(value = "用户列表",notes = "查询所有用户信息")
    public Result findUserList(@RequestParam(required = true,defaultValue = "1" )Integer current,@RequestParam(required = true,defaultValue = "8" )Integer size){

        //对用户进行分页，泛型中注入的就是用户实体类
        Page<TbUser> page = new Page<>(current,size);

        Page<TbUser>  userpage = tbUserService.page(page);
        long total = userpage.getTotal();
        List<TbUser> records = userpage.getRecords();

        return Result.ok().data("total",total).data("records",records);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个用户",notes = "通过ID查询对应的用户信息")
    public Result getUserById(@PathVariable("id") Long id){
        TbUser user = tbUserService.getById(id);
        if(user != null){
            return Result.ok().data("user",user);
        }else{
            throw new BusinessException(ResultCode.USER_NOT_FOUND_EXCEPTION.getCode(),ResultCode.USER_NOT_FOUND_EXCEPTION.getMessage());
        }

    }
}

